<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="box">
        <table>
            <thead>
                <tr>
                    <th>
                        <input type="checkbox" id="theada" />
                    </th>
                    <th>商品</th>
                    <th>价格</th>
                </tr>
            </thead>
            <tbody id="tbodya">
                <tr>
                    <td>
                        <input type="checkbox" />
                    </td>
                    <td>华为</td>
                    <td>888</td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox" />
                    </td>
                    <td>苹果</td>
                    <td>777</td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox" />
                    </td>
                    <td>小米</td>
                    <td>555</td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox" />
                    </td>
                    <td>坚果</td>
                    <td>222</td>
                </tr>

            </tbody>
        </table>

    </div>
    <script>
        //全选和取消
        // 1.获取元素
        var theada = document.getElementById("theada")//全选按钮
        var tbodya = document.getElementById('tbodya').getElementsByTagName('input')
        //绑定事件
        theada.onclick = function () {
            for (let index = 0; index < tbodya.length; index++) {
                tbodya[index].checked = this.checked
            }
        }
        // 下面复选框绑定事件
        for (let index = 0; index < tbodya.length; index++) {
            tbodya[index].onclick = function () {
                // 控制全选是否选中
                var all = true
                // 检查每个复选框是否勾选
                for (let index = 0; index < tbodya.length; index++) {
                    if (!tbodya[index].checked) {
                        all = false
                        break;//退出for循环
                    }
                }
                theada.checked = all
            }
        }


    </script>

</body>


</html>